package blazingbubble.gui.panels;




import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import blazingbubble.gui.INavigationButton;
import blazingbubble.gui.ISwitchable;
import blazingbubble.gui.PanelRegistry;
import blazingbubble.gui.Resources;
import blazingbubble.gui.elements.MenuButton;


public class MainMenuPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 844067187834205663L;
	
	public static final Image BACKGROUND_IMAGE = Toolkit.getDefaultToolkit().createImage(Resources.BACKGROUND_IMAGEPATH);
	public static final String PANEL_NAME = "MainMenu";
	
	private JoinGamePanel joinGamePanel = null;
	private HostGameDefaultOptionsPanel hostGamePanel = null;
	private SettingsPanel settingsPanel = null;
	
	private MenuButton joinButton = new MenuButton(Resources.JOINBUTTON_IMAGEPATH);
	private MenuButton hostButton = new MenuButton(Resources.HOSTBUTTON_IMAGEPATH);
	private MenuButton settingsButton = new MenuButton(Resources.SETTINGSBUTTON_IMAGEPATH);
	private MenuButton exitButton = new MenuButton(Resources.EXITBUTTON_IMAGEPATH);
	
	private ISwitchable parentFrame = null;
	
	public MainMenuPanel(ISwitchable parentFrame){
		this.parentFrame = parentFrame;
		PanelRegistry.getInstance().registerPanel(PANEL_NAME, this);
		setUp();
	}
	
	@Override
	public void paintComponent(Graphics graphics) {
		super.paintComponent(graphics);
		graphics.drawImage(BACKGROUND_IMAGE, 0, 0, getWidth(), getHeight(), this);
	}


	private void setUp(){
		this.setSize(350, 450);
		this.setLayout(null);
		
		joinGamePanel = new JoinGamePanel(parentFrame);
		hostGamePanel = new HostGameDefaultOptionsPanel(parentFrame);
		settingsPanel = new SettingsPanel(parentFrame);

		joinButton.setDestinationPanel(joinGamePanel);
		joinButton.addActionListener(this);
		this.add(joinButton);
		joinButton.setBounds(45, 150, 260, 55);
		
		hostButton.setDestinationPanel(hostGamePanel);
		hostButton.addActionListener(this);
		this.add(hostButton);
		hostButton.setBounds(45, 220, 260, 55);
		
		settingsButton.setDestinationPanel(settingsPanel);
		settingsButton.addActionListener(this);
		this.add(settingsButton);
		settingsButton.setBounds(45, 290, 260, 55);
		
		exitButton.addActionListener(this);
		this.add(exitButton);
		exitButton.setBounds(45, 360, 260, 55);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(exitButton)){
			parentFrame.shutdown();
		}
		else{
			parentFrame.switchPanel(((INavigationButton)event.getSource()).getDestinationPanel());
		}
	}	
}
